package org.jeecg.modules.foc.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.foc.entity.MDataConf;
import org.jeecg.modules.foc.service.IMDataConfService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 映射策略配置表
 * @Author: jeecg-boot
 * @Date: 2025-03-04
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "映射策略配置表")
@RestController
@RequestMapping("/foc/mDataConf")
public class MDataConfController extends JeecgController<MDataConf, IMDataConfService> {
    @Autowired
    private IMDataConfService mDataConfService;

    /**
     * 分页列表查询
     *
     * @param mDataConf
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "映射策略配置表-分页列表查询")
    @ApiOperation(value = "映射策略配置表-分页列表查询", notes = "映射策略配置表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(MDataConf mDataConf,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<MDataConf> queryWrapper = QueryGenerator.initQueryWrapper(mDataConf, req.getParameterMap());
        Page<MDataConf> page = new Page<MDataConf>(pageNo, pageSize);
        IPage<MDataConf> pageList = mDataConfService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据不同的类型获取筛选项.
     * 根据传入的值进行筛选
     */
    @AutoLog(value = "根据不同的类型获取筛选项")
    @PostMapping(value = "/getFilterOptions")
    @ApiOperation(value = "映射策略配置表-根据不同的类型获取筛选项", notes = "映射策略配置表-根据不同的类型获取筛选项")
    public Result<?> getFilterOptions(@RequestBody MDataConf mDataConf) {
        //如果没有传值，则先获取所有大类
        List<MDataConf> list = new ArrayList<>();
        switch (mDataConf.getQueryType()) {
            //查询大类
            case 1:
                LambdaQueryWrapper<MDataConf> qwBig = new QueryWrapper().lambda();
                qwBig.groupBy(MDataConf::getMpBigType);
                qwBig.select(MDataConf::getMpBigType);
                list = mDataConfService.list(qwBig);
                break;
            //查询类型
            case 2:
                LambdaQueryWrapper<MDataConf> qwDevice = new QueryWrapper().lambda();
                qwDevice.groupBy(MDataConf::getMpBigType, MDataConf::getMpDeviceType);
                qwDevice.select(MDataConf::getMpBigType, MDataConf::getMpDeviceType);
                if (mDataConf.getMpBigType() != null) {
                    qwDevice.eq(MDataConf::getMpBigType, mDataConf.getMpBigType());
                }
                list = mDataConfService.list(qwDevice);
                break;
            // 标准化信息名称
            case 3:
                LambdaQueryWrapper<MDataConf> qwPoint = new QueryWrapper().lambda();
                qwPoint.select(MDataConf::getMpBigType, MDataConf::getMpDeviceType, MDataConf::getMpPorintType, MDataConf::getMpPointName,MDataConf::getMpAlarm, MDataConf::getMpCode);
                qwPoint.groupBy(MDataConf::getMpBigType, MDataConf::getMpDeviceType, MDataConf::getMpPorintType, MDataConf::getMpPointName,MDataConf::getMpAlarm,MDataConf::getMpCode);
                if (mDataConf.getMpBigType() != null) {
                    qwPoint.eq(MDataConf::getMpBigType, mDataConf.getMpBigType());
                }
                if (mDataConf.getMpDeviceType() != null) {
                    qwPoint.eq(MDataConf::getMpDeviceType, mDataConf.getMpDeviceType());
                }
                if (mDataConf.getMpPorintType() != null) {
                    qwPoint.eq(MDataConf::getMpPorintType, mDataConf.getMpPorintType());
                }
                if (mDataConf.getMpPointName() != null) {
                    qwPoint.eq(MDataConf::getMpPointName, mDataConf.getMpPointName());
                }
                if (mDataConf.getMpAlarm() != null) {
                    qwPoint.eq(MDataConf::getMpAlarm, mDataConf.getMpAlarm());
                }
                if (mDataConf.getMpCode() != null) {
                    qwPoint.eq(MDataConf::getMpCode, mDataConf.getMpCode());
                }

                list = mDataConfService.list(qwPoint);
                break;
            default:
                return Result.error("参数错误");

        }
        return Result.OK(list);


    }


    /**
     * 添加
     *
     * @param mDataConf
     * @return
     */
    @AutoLog(value = "映射策略配置表-添加")
    @ApiOperation(value = "映射策略配置表-添加", notes = "映射策略配置表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody MDataConf mDataConf) {
        mDataConfService.save(mDataConf);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mDataConf
     * @return
     */
    @AutoLog(value = "映射策略配置表-编辑")
    @ApiOperation(value = "映射策略配置表-编辑", notes = "映射策略配置表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<?> edit(@RequestBody MDataConf mDataConf) {
        mDataConfService.updateById(mDataConf);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "映射策略配置表-通过id删除")
    @ApiOperation(value = "映射策略配置表-通过id删除", notes = "映射策略配置表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        mDataConfService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "映射策略配置表-批量删除")
    @ApiOperation(value = "映射策略配置表-批量删除", notes = "映射策略配置表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.mDataConfService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "映射策略配置表-通过id查询")
    @ApiOperation(value = "映射策略配置表-通过id查询", notes = "映射策略配置表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        MDataConf mDataConf = mDataConfService.getById(id);
        return Result.OK(mDataConf);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param mDataConf
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MDataConf mDataConf) {
        return super.exportXls(request, mDataConf, MDataConf.class, "映射策略配置表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MDataConf.class);
    }

}
